package com.xcfw.dorm.seviceimpl.dormcenter;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xcfw.dorm.entity.Assistant;
import com.xcfw.dorm.entity.NumEnum;
import com.xcfw.dorm.entity.ReturnJson;
import com.xcfw.dorm.exception.ServiceException;
import com.xcfw.dorm.mapper.AssistantMapper;
import com.xcfw.dorm.sevice.BaseService;
import com.xcfw.dorm.sevice.ZSGCService;
import com.xcfw.dorm.util.DormUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: Dorm
 * @description: 辅导员
 * @author: Bruse Queen
 * @create: 2018-04-07 17:12
 **/
@Service("AssistantServiceImpl")
public class AssistantServiceImpl extends BaseService implements ZSGCService {
    @Autowired
    private AssistantMapper mapper;

    @Override
    public void add(Object o) {
        Assistant param = (Assistant) o;
        Integer i = mapper.selectAutoId();
        if (param.getSchoolId() == null) {
            if (null != i) {
                param.setSchoolId(i + 1);
            } else {
                param.setSchoolId(1);
            }
        }
        param.setUuid(DormUtil.getUUID());
        zsgcStatus = mapper.insertSelective(param);
        if (1 != zsgcStatus) {
            throw new ServiceException("新增辅导员信息失败！");
        }
    }

    @Override
    public void delete(Integer id) {
        zsgcStatus = mapper.deleteByPrimaryKey(id);
        if (1 != zsgcStatus) {
            throw new ServiceException("删除辅导员信息失败！");
        }
    }

    @Override
    public void update(Object o) {
        Assistant param = (Assistant) o;

        Assistant temp = mapper.selectByPrimaryKey(param.getId());
        if (temp != null) {
            if (param.equals(temp)) {
                throw new ServiceException("您未修改任何信息！");
            }
        }
        zsgcStatus = mapper.updateByPrimaryKeySelective(param);
        if (1 != zsgcStatus) {
            throw new ServiceException("修改辅导员信息失败！");
        }
    }

    @Override
    public Object select(Object o) {

        Assistant param = (Assistant) o;

        Integer schoolId = param.getSchoolId();
        if ("".equals(param.getName())) {
            param.setName(null);
        }

        ReturnJson result;
        if (param.getPageNum() == null || param.getPageSize() == null) {
            List<Assistant> list = mapper.selectActionSql(DormUtil.setNull(schoolId), param.getName(),null);
            if (list.size() == 0) {
                throw new ServiceException("暂无相关数据！");
            }
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "辅导员信息查询成功！", list, list.size());
        } else {
            PageHelper.startPage(param.getPageNum(), param.getPageSize());
            Page<Assistant> list = (Page<Assistant>) mapper.selectActionSql(DormUtil.setNull(schoolId), param.getName(),null);
            if (list.size() == 0) {
                throw new ServiceException("暂无相关数据！");
            }
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "辅导员信息查询成功！", list, list.getTotal());
        }

        return result;
    }
}
